﻿using Alipay.AopSdk.Core;
using Alipay.AopSdk.Core.Request;
using Alipay.AopSdk.Core.Response;
using Dapper_Core.Model;
using System;
using System.Collections.Generic;
using System.Text;

namespace Dapper_Core.Common
{
    public class AliPayUtility
    {
        /// <summary>
        /// 发起支付
        /// </summary>
        /// <param name="out_trade_no">订单号</param>
        /// <param name="amount">订单金额（元）</param>
        /// <param name="body">对一笔交易的具体描述信息。如果是多种商品，请将商品描述字符串累加传给body。</param>
        /// <param name="subject">商品简单描述</param>
        /// <param name="setNotifyUrl">支付宝后台通知的地址</param>
        /// <param name="setReturnUrl">支付宝前台回跳的地址</param>
        /// <param name="isdev">0沙箱 1正式（默认）</param>
        /// <returns>正常返回form表单,异常返回值  "异常：" + ex.ToString() </returns>
        public static string Alipay_gateway_do(string out_trade_no, string amount, string body, string subject, string setNotifyUrl, string setReturnUrl, int isdev = 1)
        {
            try
            {
                string timeout_express = "30m";//订单有效时间（分钟）
                string postUrl = "";
                string sign_type = "RSA2";//加签方式 有两种RSA和RSA2 我这里使用的RSA2（支付宝推荐的）
                string version = "1.0";//固定值 不用改
                string format = "json";//固定值
                string method = "QUICK_WAP_WAY";//调用接口 固定值 不用改
                if (isdev == 1)
                {
                    postUrl = "https://openapi.alipay.com/gateway.do";
                }
                else
                {
                    postUrl = "https://openapi.alipaydev.com/gateway.do";
                }
                IAopClient client = new DefaultAopClient(postUrl, Appsettings.AlipayAppId, Appsettings.AlipayMerchantPrivateKey, format, version, sign_type, Appsettings.AlipayPublicKey, "UTF-8", false);
                AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
                if (!string.IsNullOrEmpty(setNotifyUrl))
                {
                    request.SetNotifyUrl(setNotifyUrl);//支付宝后台通知的地址
                }
                if (!string.IsNullOrEmpty(setReturnUrl))
                {
                    request.SetReturnUrl(setReturnUrl);//支付宝前台回跳的地址
                }
                request.BizContent = "{" +
                                     "    \"body\":\"" + body + "\"," +
                                     "    \"subject\":\"" + subject + "\"," +
                                     "    \"out_trade_no\":\"" + out_trade_no + "\"," +
                                     "    \"timeout_express\":\"" + timeout_express + "\"," +
                                     "    \"total_amount\":" + amount + "," +
                                     "    \"product_code\":\"" + method + "\"" +
                                     "}";
                AlipayTradeWapPayResponse response = client.pageExecute(request);
                string form = response.Body.Substring(0, response.Body.IndexOf("<script>"));
                return form;
            }
            catch (Exception ex)
            {
                return "异常：" + ex.ToString();
            }
        }

        /// <summary>
        /// 查询交易记录
        /// </summary>
        /// <param name="out_trade_no">订单号</param>
        /// <param name="isdev">0沙箱 1正式（默认）</param>
        /// <returns></returns>
        public static string Alipay_geteway_do_query(string out_trade_no, int isdev = 1)
        {
            string postUrl = "";
            string sign_type = "RSA";//加签方式 有两种RSA和RSA2 我这里使用的RSA2（支付宝推荐的）
            string version = "1.0";//固定值 不用改
            string format = "json";//固定值
            if (isdev == 1)
            {
                postUrl = "https://openapi.alipay.com/gateway.do";
            }
            else
            {
                postUrl = "https://openapi.alipaydev.com/gateway.do";
            }
            IAopClient client = new DefaultAopClient(postUrl, Appsettings.AlipayAppId, Appsettings.AlipayMerchantPrivateKey, format, version, sign_type, Appsettings.AlipayPublicKey, "UTF-8", false);
            var request = new AlipayTradeQueryRequest();
            request.BizContent = "{" + "\"out_trade_no\":\"" + out_trade_no + "\"}";
            AlipayTradeQueryResponse response = client.Execute(request);
            string form = response.Body;
            return form;
        }
    }
}
